﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace ViewMaker.Core.Controls
{
    /// <summary>
    /// ハイパーリンクViewコントロール
    /// </summary>
    public class HyperlinkViewControl : HeaderViewControl
    {
        /// <summary>
        /// プロパティ定数
        /// </summary>
        public new class Properties : HeaderViewControl.Properties
        {
            public const string Command = "Command";
            public const string NavigateUri = "NavigateUri";
            public const string DisableRequestNavigation = "DisableRequestNavigation";
        }

        /// <summary>
        /// リンクを選択した時に実行するコマンド
        /// </summary>
        [Display(GroupName = CommandGroup)]
        [View(ViewControlType.ComboBox)]
        [ViewProperty(ComboBoxViewControl.Properties.ItemsSource, "Item.CommandList")]
        [ViewProperty(ComboBoxViewControl.Properties.IsEditable, true)]
        public string Command { get; set; }

        /// <summary>
        /// ナビゲーションURI
        /// </summary>
        public string NavigateUri { get; set; }

        /// <summary>
        /// RequestNavigation時にナビゲーションURIを起動するビヘイビアを設定しないか
        /// </summary>
        [Display(Name = "Disable Navigation")]
        [ViewProperty(ViewControl.Properties.IsEnabled, "IsWpf")]
        public bool DisableRequestNavigation { get; set; }

        public HyperlinkViewControl()
        {
            ControlType = ViewControlType.Hyperlink;
        }

        /// <summary>
        /// Xamlを生成する
        /// </summary>
        /// <param name="parent"></param>
        /// <returns></returns>
        public override List<XamlNode> CreateXaml(XamlNode parent)
        {
            var list = base.CreateXaml(parent);
#if !SILVERLIGHT
            var node = parent.CreateElement("Label");
            var link = node.AddElement("Hyperlink");
            var txt = link.AddElement("TextBlock");
            SetBindingAttribute(txt, "Text", Item.BindPath, true);
            if (!string.IsNullOrEmpty(Command))
            {
                SetBindingAttribute(link, "Command", Command, true);
            }
            else 
            {
                SetBindingAttribute(link, "NavigateUri", NavigateUri ?? Item.BindPath, true);
#if !SILVERLIGHT
                if (!DisableRequestNavigation) link.SetAttribute("vg:RequestNavigateBehavior.IsEnabled", "clr-namespace:ViewMaker.Core.Wpf;assembly=ViewMaker.Core", "true");
#endif
            }
            ApplyBaseProperty(node);
            node.SetAttribute("Name", null);
            link.InsertAttribute(0, "Name", this.Item.Name);
#else
            var node = parent.CreateElement("HyperlinkButton");
            SetBindingAttribute(node, "Content", Item.BindPath, true);

            if (!string.IsNullOrEmpty(Command))
            {
                SetBindingAttribute(node, "Command", Command, true);
            }
            else 
            {
                SetBindingAttribute(node, "NavigateUri", NavigateUri ?? Item.BindPath, true);
            } 
            ApplyBaseProperty(node);
#endif
            list.Add(node);
            return list;
        }

    }

}
